﻿using VIFON.BLL;
using VIFON.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using System.Linq;
namespace VIFON
{
    public partial class frmPaymentType : Telerik.WinControls.UI.RadForm
    {
        public frmPaymentType()
        {
            InitializeComponent();
        }
        BLPaymentType blPaymentType = new BLPaymentType();
        int paymenttypeid = int.MinValue;
        private void frmPaymentType_Load(object sender, EventArgs e)
        {
            LoadPaymentType();
        }
        private void LoadPaymentType()
        {
            grvpayment.Rows.Clear();
            var list = blPaymentType.GetPaymentList();
            if(list!=null)
            {
                foreach (var l in list)
                {
                    grvpayment.Rows.Add(l.Id, l.Name);
                }
            }
            
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            
            if(txtpaymentname.Text=="")
            {
                MessageBox.Show("Nhập tên hình thức thanh toán", "Thông báo");
                txtpaymentname.Focus();
            }
            else
            {
                PaymentType boPaymentType=new PaymentType
                {
                    Id=paymenttypeid,
                    Name=txtpaymentname.Text
                };
                if (blPaymentType.Insert(boPaymentType))
                    MessageBox.Show(Common.ProcessSuccess, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                    MessageBox.Show(Common.ProcessError, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            LoadPaymentType();
        }

        private void grvpayment_CellClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {
            try
            {
                int rowindex = grvpayment.CurrentCell.RowIndex;
                paymenttypeid = int.Parse(grvpayment.Rows[rowindex].Cells[0].Value.ToString());
                txtpaymentname.Text = grvpayment.Rows[rowindex].Cells[1].Value.ToString();
            }
            catch { }                
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            PaymentType boPaymentType = new PaymentType
            {
                Id = paymenttypeid,
                Name = txtpaymentname.Text
            };
            if (blPaymentType.Update(boPaymentType))
                MessageBox.Show(Common.ProcessSuccess, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show(Common.ProcessError, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
            LoadPaymentType();
            txtpaymentname.Text = string.Empty;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (blPaymentType.Delete(paymenttypeid))
                MessageBox.Show(Common.ProcessSuccess, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show(Common.ProcessError, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
            LoadPaymentType();
            txtpaymentname.Text = string.Empty;
        }
    }
}
